<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - geometry - cube</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
	</head>
	<body>

		<script type="module">

			import * as THREE from '../build/three.module.js';
			import Stats from './jsm/libs/stats.module.js';
			import { TDSLoader } from './jsm/loaders/TDSLoader.js';
			import { TrackballControls } from './jsm/controls/TrackballControls.js';


			let camera, scene, renderer;
			let elf,controls;

			init();
			animate();

			function init() {

				//初始化
				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
				camera.position.z = -1500;
				

				scene = new THREE.Scene();

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.shadowMap.enabled = true;
				renderer.shadowMap.type = THREE.PCFSoftShadowMap;
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				document.body.appendChild( renderer.domElement );

				//浏览方式
				controls = new TrackballControls( camera, renderer.domElement );

				window.addEventListener( 'resize', onWindowResize, false );
				
				
				const ambientLight = new THREE.AmbientLight( 0xffffff, 0.4 );
				scene.add( ambientLight );

				//设置灯光
				let directionalLight = new THREE.DirectionalLight( 0xffffff, 0.8 );
				directionalLight.position.set( 200, 450, -500 );


				directionalLight.castShadow = true; // default false
				scene.add( directionalLight );


				directionalLight.castShadow = true;

				directionalLight.shadow.mapSize.width = 1024;
				directionalLight.shadow.mapSize.height = 512;

				directionalLight.shadow.camera.near = 100;
				directionalLight.shadow.camera.far = 2500;

				directionalLight.shadow.camera.left = - 1000;
				directionalLight.shadow.camera.right = 1000;
				directionalLight.shadow.camera.top = 1000;
				directionalLight.shadow.camera.bottom = - 1000;


				//添加阴影的辅助线
				//let helper = new THREE.CameraHelper( directionalLight.shadow.camera );
				//scene.add( helper );//
					

				//加载模型，并且设置阴影
				let loader = new TDSLoader(  );				
				loader.load( 'models/test.3ds', function ( object ) {
				
						elf = object;	

						object.traverse( function ( child ) {

						if ( child.isMesh ) {

							child.castShadow = true;

						}

					} );


						scene.add(object);
								

				} );
			
			  
				//加载地面
				const gt = new THREE.TextureLoader().load( "models/textures/terrain/grasslight-big.jpg" );
				const gg = new THREE.PlaneBufferGeometry( 2500, 2500 );
				const gm = new THREE.MeshPhongMaterial( { color: 0xffffff, map: gt } );

				const ground = new THREE.Mesh( gg, gm );
				ground.rotation.x = - Math.PI / 2;
				ground.position.y = -800;
				ground.material.map.repeat.set( 64, 64 );
				ground.material.map.wrapS = THREE.RepeatWrapping;
				ground.material.map.wrapT = THREE.RepeatWrapping;
				ground.material.map.encoding = THREE.sRGBEncoding;
				// note that because the ground does not cast a shadow, .castShadow is left false
				ground.receiveShadow = true;
				scene.add(ground);			
				


			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				requestAnimationFrame( animate );
				controls.update();				

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
